// pages/task/task.js
const app = getApp()
const util = require('../../utils/util.js')
const Debug = require('../../utils/Debug.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    num: "1",
    title: "任务中心", //当前页面的标题
    daynum: "",
    loginshow: false,
        tishishow:false,//我的小程序教程
          xcxshow: false,//我的小程序教程2
          toastshow:false//完成我的小程序任务
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      headerheight: app.globalData.navHeight
    })
  },
  onShow: function() {
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          this.setData({
            loginshow: true
          })
        }else{
          this.setData({
            loginshow: false
          })
        }
      }
    })
    this.request()    
  },
  
  login: function() {
    var that = this
    wx.login({
      success: function(res) {
        var code = res.code
        var data = {
          userCode: code,
          userImageUrl: "",
          userName: ""
        }
        util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
      }
    })
  },
  succeed: function(res) {
    wx.setStorageSync('tekon', res.data.token)
    wx.setStorageSync('userInfo', res.data.user)
    this.request()
  },
  request: function() {
    wx: wx.showNavigationBarLoading()
    wx.showLoading({
      title: '玩命加载中...',
    })
    var header = {
      'authorization': wx.getStorageSync("tekon"),
      'content-type': "application/x-www-form-urlencoded"
    }
    util.formhttp(app.globalData.appurl + "/mission", 'get', '', header, this.stat);
  },
  stat: function(res) {
          Debug.debug("《------任务列表-----》")
          Debug.debug(res)
    if (res.status == 401) {
      this.login()
    }
    wx: wx.hideNavigationBarLoading()
    wx.hideLoading()

    if (res.data.checkInDays == 7) {
      this.setData({
        qiandao: 0,
      })
    } else {
      this.setData({
        qiandao: res.data.checkInDays,
      })
    }
        var newlist = res.data.userMissions.newerMissions.concat(res.data.userMissions.commonMissions).concat(res.data.userMissions.dailyMissions)
        // 显示列表
         var  showlist=[]
          for (var j = 0; j < newlist.length; j++) {
                  if (newlist[j].hidden == 0) {
                          showlist.push(newlist[j])
                  }
          }
          Debug.debug("《------任务显示列表-----》")
          Debug.debug(showlist)
        var ioslist=[]
          for (var k = 0; k < showlist.length;k++){
                  if (showlist[k].platform==0){
                          ioslist.push(showlist[k])
                  }
          }
          Debug.debug("《------苹果任务列表-----》")
          Debug.debug(ioslist)
          if (app.globalData.platform=="ios"){
                  this.setData({
                          rcrw: ioslist
                  })
        }else{
                  this.setData({
                          rcrw: showlist
                  })
        }
        
    this.setData({
      day: res.data.userMissions.checkInMissions,
      daynum: res.data.checkInDays
    })

    if (res.data.isCheckedToday == false) {
            this.setData({
                    layershow:true
            })
    }
  },

        jiaocheng:function(e){
                console.log(e.target.dataset)
                if (e.target.dataset.id == "15") {
                        // 收藏
                        this.setData({
                                imgindex:5,
                                kefushow:true,
                                butshow:false
                        })
                } else if (e.target.dataset.id == "16") {
                        // 收藏
                        this.setData({
                                imgindex: 2,
                                kefushow: true,
                                butshow: true
                        })
                } else if (e.target.dataset.id == "13") {
                        // 添加到桌面
                        this.setData({
                                imgindex: 6,
                                kefushow: true,
                                butshow: false
                        })
                } else if (e.target.dataset.id == "17") {
                        // 添加我的小程序
                        this.setData({
                                tishishow: true
                        })
                } else if (e.target.dataset.id == "18") {
                        // 添加公众号
                        this.setData({
                                imgindex: 7,
                                kefushow: true,
                                butshow: true
                        })
                }
        },
       
        cha:function(){
                this.setData({
                        tishishow: false,
                        xcxshow:true      
                })
        },
        xiaowodexcxcha:function(){
                this.setData({
                        xcxshow: false
                })
                var header = {
                        'authorization': wx.getStorageSync("tekon"),
                        'content-type': "application/x-www-form-urlencoded"
                }
                var data = {
                        missionId: 17
                }
                util.formhttp(app.globalData.appurl + "/mission", 'POST', data, header, (res)=>{
                        console.log(res)
                        var that = this
                        if (res.data.missionAccomplished == 1) {
                                that.setData({
                                        toastshow: true,
                                        contens: "奖励添加我的小程序的你",
                                        jinbi: res.data.missionReward
                                })
                                setTimeout(function () {
                                        that.setData({
                                                toastshow: false
                                        })
                                }, 3000)
                        }
                         if (res.message == "success"){
                                this.request()
                        }
                })
        },



  links: function(e) {
    // console.log(e.currentTarget.dataset.url)
    if (e.currentTarget.dataset.url == '/pages/index/index') {
      wx.switchTab({
        url: e.currentTarget.dataset.url,
      })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },
  botshow: function(e) {
    // console.log(e.currentTarget.dataset.num)
    if (e.currentTarget.dataset.num == this.data.num) {
      this.setData({
        num: 0
      })
    } else {
      this.setData({
        num: e.currentTarget.dataset.num
      })
    }
  }
})